import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index.vue'
import store from '../store/index'
//处理重定向抛出异常问题
const a = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return a.call(this, location).catch(err => err);
}
Vue.use(VueRouter)
const routes = [{
    path: '/',
    name: 'Index',
    component: Index,
  },
  {
    path: '/category',
    name: 'Cateogry',
    component: () => import('../views/Category')
  },
  {
    path: '/cart',
    name: 'Cart',
    meta: {
      needLogin: true
    }, //标记当前路由是否需要登录之后才能进入
    component: () => import('../views/Cart')
  },
  {
    path: '/my',
    name: 'My',
    meta: {
      needLogin: true
    },
    component: () => import('../views/My')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Search')
  },
  {
    path: '/news_list',
    name: 'News_list',
    component: () => import('../views/News_list')
  },
  {
    path: '/recommend/:id',
    name: 'Recommend',
    component: () => import('../views/Recommend')
  },
  {
    path: '/promotion',
    name: 'Promotion',
    component: () => import('../views/Promotion')
  },
  {
    path: '/user/get_coupon',
    name: 'Get_coupon',
    component: () => import('../views/Get_coupon')
  },

  {
    path: '/user/sign',
    name: 'Sign',
    meta: {
      needLogin: true
    },
    component: () => import('../views/Sign')
  },
  {
    path: '/collection',
    name: 'Collection',
    meta: {
      needLogin: true
    },
    component: () => import('../views/Collection')
  },
  {
    path: '/activity/goods_seckill',
    name: 'Goods_seckill',
    component: () => import('../views/Goods_seckill')
  },
  {
    path: '/goods_list',
    name: 'Goods_list',
    component: () => import('../views/Goods_list')
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: () => import('../views/Detail')
  },
  {
    path: '/user/add_manage',
    name: 'Add_manage',
    meta: {
      needLogin: true
    },
    component: () => import('../views/Add_manage')
  },
  {
    path: '/order/list/',
    name: 'OrderList',
    meta: {
      needLogin: true
    },
    component: () => import('../views/OrderList')
  },
  {
    path: '/evaluate',
    name: 'Evaluate',
    meta: {
      needLogin: true
    },
    component: () => import('../views/Evaluate')
  },
  {
    path: '/order/submit/:id',
    name: 'OrderSubmit',
    meta: {
      needLogin: true
    },
    component: () => import('../views/OrderSubmit')
  },
]

const router = new VueRouter({
  mode: 'hash',
  routes
})
router.beforeEach((to, from, next) => { //全局路由守卫.
  // console.log('needLogin', to.meta.needLogin);
  // console.log('isLogin==>', store.getters['isLogin']);
  //to.meta.needLogin 需要登录  isLogin 没有登录返回false
  if (to.meta.needLogin && !store.getters['isLogin']){
    next('/login' + `?path=${to.path}`);
    return;
  }
  next();
});
export default router